<?php
/**
 * Created by PhpStorm.
 * User: saeb
 * Date: 2/11/19
 * Time: 9:47 AM
 */

if (!defined('ABSPATH'))exit('Diee');

if (!function_exists('err_log')) {
    function err_log($obj)
    {
        error_log(
            print_r($obj, true)
        );
    }
}
function gf_ml_add_menu_page()
{
    add_menu_page(
        __('Gravity forms multi limits', 'gf-ml'),
        __('Gf-Limits', 'gf-ml'),
        'level_0',
        'gf_ml',
        'gf_ml_main_page'
    );
    add_submenu_page(
        'gf_ml',
        __p('Reset'),
        __p('Reset'),
        'level_3',
        'gf_ml_delete_all_limits',
        'gf_ml_limits_list_page'
    );
}
function gf_ml_main_page()
{

    echo '<div class="wrap">';

    $forms = GFAPI::get_forms();
    $forms_array = array();
    foreach ($forms as $form) {
        $id = $form['id'];
        $title = $form['title'];
        $forms_array["$id"] = $title;
    }
    ?>
    <span id="select-form" style="display: table-row-group;">
        <div id="select_form_selector_block">
    <label for="gf_ml_select_form"><?php echo esc_html(__p('select your form')); ?></label>
<select id="gf_ml_select_form" onchange="selected_form_id()"
        style="float: <?php echo is_rtl() ? 'right' : 'left'; ?>;">

    <?php
    foreach ($forms_array as $id => $title) {
        echo("<option value=\""
            . esc_html($id)
            . "\" id=\"gf_ml_formis_"
            . esc_html($id) .
            "\">" . esc_html($title)
            . "</option>");
    }
    ?>
</select>
    </div>
        <div>
        <label for="add_new_button_holder_0"></label>
        <img src=" <?php echo esc_url(gf_ml_url); ?>assets/img/pluss.svg"
             id="btn_img_0" class="btn_img"
             onclick="newfield(window.fields_array)"
             style="float: <?php echo esc_html(is_rtl() ? 'right' : 'left'); ?>;"
        >
            </div>
        <div id="value_limit_holder"
             style=" float: <?php echo is_rtl() ? 'right' : 'left'; ?>;">
        <div id="value_limit_input"
             style="float: <?php echo is_rtl() ? 'right' : 'left'; ?>;">
            <label for="limit_input_1"><?php echo esc_html(__p('limitation is')); ?> </label>
            <input type="number" maxlength="4" id='limit_input_1' onchange="(jQuery('#submit_btn').show())">
        </div>

        <div id="value_limit_btn"
             style="display: inline-block; float: <?php echo is_rtl() ? 'right' : 'left'; ?>;"
        >
            <div>
            <label for="submit_btn" ></label>
        <button id="submit_btn" style="display: none;"
                onclick="get_limits()"><?php echo esc_html(__p('submit')); ?></button>
                </div>
        </div>
        </div>
    </span><br>
    <hr>
    <div id="all_containers">

    </div>


    <?php
    echo "</div>";// container


    wp_enqueue_script('gfmlmainjs',
        gf_ml_url . 'assets/js/gfmlmain.js',
        array('jquery'), true, true);
    wp_localize_script('gfmlmainjs', 'gf_ml_plugin', array('pluginurl' => gf_ml_url));
//    wp_enqueue_script('bootstrap.min',
//        gf_ml_url . 'assets/js/bootstrap.min.js',
//        array('jquery'), true, true);
    wp_enqueue_style('gfmlmainstyle', gf_ml_url . 'assets/css/gfmlmain.css');
}
add_action('admin_menu', 'gf_ml_add_menu_page');
function gf_ml_limits_list_page(){
    if (isset($_GET['deleteall']) and isset($_GET['_wpnonce'])) {
        if (wp_verify_nonce($_GET['_wpnonce'], -1)) {
            require_once gf_ml_dir.'includes/gf_ml_wpdb.php';
            $result =gf_ml_deactive_all();
            echo "$result";
            echo __p('of limit option deactived');
        }
    }
    ?>

    <div id="deactivate_limits_link">
        <?php //wp_nonce_field( 'deletealloptions', 'delete_nonce' ); ?>
        <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=gf_ml_delete_all_limits&deleteall=1'))); ?>">
            <?php echo esc_html(__p('Delete All limits')); ?>
        </a>
    </div>
<?php
    echo esc_html(__p('deactive all options'));
    require_once gf_ml_dir.'includes/gf_ml_validation.php';
    $limits = gf_ml_get_limits();
    foreach ($limits as $l){
        try {
            $form_title = GFAPI::get_form($l['form_id'])['title'];
            if ($form_title==null) gf_ml_deactive_form_data($l['form_id']);
            continue;
            #lets complete this later
        }
        catch (Exception $e) {
            //err_log($e);
        }
    }
//    var_dump($limits);
}